package com.example.nacos.utils;

import com.example.nacos.config.SeparatorAnnotation;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
public class FileReaderUtil {

    /**
     * 读取CSV文件并转换为对象列表
     */
    public static <T> List<T> readCsvFile(String filePath, Class<T> clazz) throws IOException {
        List<T> result = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath, StandardCharsets.UTF_8))) {
            String line;
            boolean isFirstLine = true;
            List<String> headers = new ArrayList<>();

            while ((line = br.readLine()) != null) {
//                if (isFirstLine) {
//                    headers = Arrays.asList(line.split(","));
//                    isFirstLine = false;
//                    continue;
//                }

                T obj = mapLineToObject(line, clazz);
                if (obj != null) {
                    result.add(obj);
                }
            }
        }
        return result;
    }

    /**
     * 读取文本文件并转换为对象列表（每行一个对象）
     */
    public static <T> List<T> readTextFile(String filePath, Class<T> clazz) throws IOException {
        List<T> result = new ArrayList<>();
        List<String> lines = Files.readAllLines(Paths.get(filePath), StandardCharsets.UTF_8);

        for (String line : lines) {
            log.info("line: {}", line);
            T obj = mapLineToObject(line, clazz);
            if (obj != null) {
                result.add(obj);
            }
        }
        return result;
    }

    /**
     * 使用Java 8 Stream方式读取文件
     */
    public static <T> List<T> readFileWithStream(String filePath, Class<T> clazz) throws IOException {
        try (Stream<String> lines = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
            return lines
                    .skip(1) // 跳过标题行
                    .map(line -> mapLineToObject(line, clazz))
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
        }
    }
    /**
     * 将单行文本映射为对象
     */
    private static <T> T mapLineToObject(String line, Class<T> clazz) {
        // 这里可以根据具体格式实现映射逻辑
        // 例如: 使用分隔符分割后映射到对象属性
        log.info("clazz: {}", clazz.getName());
        // 检查类是否有ClassInfo注解
        if (clazz.isAnnotationPresent(SeparatorAnnotation.class)) {
            // 获取注解实例
            SeparatorAnnotation annotation = clazz.getAnnotation(SeparatorAnnotation.class);

            log.info("=== " + clazz.getSimpleName() + " 类注解信息 ===");
            log.info("作者: " + annotation.value());
            log.info("版本: " + annotation.description());
            return (T) LineMapper.mapLineToObject(line, annotation.value(), clazz);
        } else {
            log.info("类 " + clazz.getSimpleName() + " 没有 SeparatorAnnotation 注解\n");
            return null;
        }




    }
}
